package com.shadow.manager
{
	import com.shadow.constant.SystemKey;
	import com.shadow.dataentities.User;
	import com.shadow.dataentities.systemdata.BuildingEntity;
	import com.shadow.dataentities.userdata.UserInfoEntity;
	import com.shadow.manager.dataManager.ShopBuildingsManager;
	import com.shadow.ui.objects.AssetFactory;
	import com.shadow.ui.objects.base.Asset;
	
	import flash.net.FileFilter;
	
	import mx.core.Singleton;

	/**
	 *
	 * @author Administrator
	 * 
	 */	
	public class ModuleManager
	{
		private static var _instance:ModuleManager;
		private var _user:User;
		private var _user_info:UserInfoEntity;
		private var _user_buildings:Object;
		private var _friend_list:Array;
		private var _tutorialData:Object;
		public function ModuleManager(value:Singleton)
		{
			_user_buildings = new Object();
			_friend_list = new Array();
			
		}
		
		public static function getInstance():ModuleManager
		{
			if(ModuleManager._instance==null)
			{
				ModuleManager._instance = new ModuleManager(new Singleton());
			}
			
			return ModuleManager._instance;
		}
		
		/**
		 *初始化数据 
		 * @param data
		 * 
		 */		
		public function setInitialData(serverData:Object):void
		{
			TimerManager.getInstance().serverTimestamp = ((serverData.ts)!=null) ? (serverData.ts * 1000) : new Date().time;
			setUserData(serverData.data.user);
			_user_info = new UserInfoEntity(serverData.data.user_info);
			setFriends(serverData.data.friends);
			setUserBuildings(serverData.data.user_buildings);
			setTutorialData(serverData.data.tutorials);
		}
		/**
		 *获取用户 
		 * @return 
		 * 
		 */		
		public function get user():User
		{
			return this._user;
		}
		
		
		/**
		 *获取用户建筑集合 
		 * @return 
		 * 
		 */		
		public function get getUserBuildings():Object
		{
			return this._user_buildings;
		}
		
		/**
		 *获取教程数据 
		 * @return 
		 * 
		 */		
		public function get getTutorialData():Object
		{
			return this._tutorialData;
		}
		
		/**
		 *设置用户数据 
		 * @param userData
		 * 
		 */		
		private function setUserData(userData:Object):void
		{
			var key:String;
			this._user = new User();
			for (key in userData)
			{
				if (this._user.hasOwnProperty(key.toString()))
				{
					this._user[key.toString()] = userData[key];
				}
			}
		}
		/**
		 *设置好友数据 
		 * @param friends
		 * 
		 */		
		private function setFriends(friends:Object):void
		{
			var friend:Object;
			var friendData:User; 
			if(friends.length>0){
				for each(friend in friends){
					friendData = makeFriendData(friend);
					_friend_list.push(friendData);
					
				}
			}
		}
		
		private function makeFriendData(friend_data:Object):User
		{
			var key:*;
			var friend:User = new User();
			for (key in friend_data)
			{
				if (friend.hasOwnProperty(key.toString()))
				{
					friend[key.toString()] = friend_data[key];
				}
			}
			return friend;
		}
		
		/**
		 *设置用户建筑数据 
		 * @param userBuildings
		 * 
		 */		
		private function setUserBuildings(userBuildings:Object):void
		{
			var buildingEntity:BuildingEntity;
			var userItem:Object;
			var building:Asset;
			for each(userItem in userBuildings)
			{
				buildingEntity = ShopBuildingsManager.getInstance().getBuildByID(userItem.item_id);
				if(buildingEntity.system_key!=SystemKey.BUILDING_SYSTEM_KEY){
					return;
				}
				
				if(userItem.health<0){
					userItem.health =0;
				}
				
				building = AssetFactory.createAsset(buildingEntity,userItem);
				_user_buildings[userItem.id.toString()] = building;
				
			}
		}
		
		private function setTutorialData(tutorials:Object):void
		{
			if(tutorials!=null){
				_tutorialData  = tutorials;
			}	
		}
		
	}
}

class Singleton 
{
	
	
}

